package com.item.util;

import java.util.Calendar;

import com.base.cache.CacheSupport;
import com.base.support.PropertySupport;

public class CacheController {
	private static String Cache_NAME = "frameCacheZSND";
	
	private static String JW_SIGNAL = "_JW";	//教务处缓存标志
	private static String GJW_SIGNAL = "_GJW";	//图书馆缓存标志
	private static String LIB_SIGNAL = "_LIB";	//图书馆缓存标志
	private static String ECARD_SIGNAL = "_ECard";	//一卡通缓存标志
	private static String FLOWER_SIGNAL = "_FLOWER";
	
	private static String ECARD_STATE_SINGAL = "_ECardView";
	
	private static String ECARD_REINPUT = "_ECR";
	private static String JW_REINPUT = "_JWR";
	private static String GJW_REINPUT = "_GJWR";
	
	/**
	 * 添加教务处缓存
	 * @param userid
	 * @param value
	 */
	public static void addJWCache(String userid, Object value) {
		CacheSupport.put(Cache_NAME, userid + JW_SIGNAL, value);
	}
	
	/**
	 * 删除教务处缓存
	 * @param userid
	 */
	public static void deleteJWCache(String userid) {
		CacheSupport.remove(Cache_NAME, userid + JW_SIGNAL);
	}
	
	/**
	 * 获取教务处客户端查询缓存
	 * @param userid
	 * @param c
	 * @return
	 */
	public static <T> T getJWCache(String userid, Class<T> c) {
		return CacheSupport.get(Cache_NAME, userid + JW_SIGNAL, c);
	}
	
	/**
	 * 添加教务处户端查询缓存
	 * @param userid
	 * @param value
	 */
	public static void addJWRCache(String userid, Object value) {
		CacheSupport.put(Cache_NAME, userid + JW_REINPUT, value);
	}
	
	/**
	 * 删除教务处户端查询缓存
	 * @param userid
	 */
	public static void deleteJWRCache(String userid) {
		CacheSupport.remove(Cache_NAME, userid + JW_REINPUT);
	}
	
	/**
	 * 获取教务处缓存
	 * @param userid
	 * @param c
	 * @return
	 */
	public static <T> T getJWRCache(String userid, Class<T> c) {
		return CacheSupport.get(Cache_NAME, userid + JW_REINPUT, c);
	}
	
	/**
	 * 添加教务处缓存
	 * @param userid
	 * @param value
	 */
	public static void addGJWCache(String userid, Object value) {
		CacheSupport.put(Cache_NAME, userid + GJW_SIGNAL, value);
	}
	
	/**
	 * 删除教务处缓存
	 * @param userid
	 */
	public static void deleteGJWCache(String userid) {
		CacheSupport.remove(Cache_NAME, userid + GJW_SIGNAL);
	}
	
	/**
	 * 获取教务处客户端查询缓存
	 * @param userid
	 * @param c
	 * @return
	 */
	public static <T> T getGJWRCache(String userid, Class<T> c) {
		return CacheSupport.get(Cache_NAME, userid + GJW_REINPUT, c);
	}
	
	/**
	 * 添加教务处户端查询缓存
	 * @param userid
	 * @param value
	 */
	public static void addGJWRCache(String userid, Object value) {
		CacheSupport.put(Cache_NAME, userid + GJW_REINPUT, value);
	}
	
	/**
	 * 删除教务处户端查询缓存
	 * @param userid
	 */
	public static void deleteGJWRCache(String userid) {
		CacheSupport.remove(Cache_NAME, userid + GJW_REINPUT);
	}
	
	/**
	 * 获取教务处缓存
	 * @param userid
	 * @param c
	 * @return
	 */
	public static <T> T getGJWCache(String userid, Class<T> c) {
		return CacheSupport.get(Cache_NAME, userid + GJW_SIGNAL, c);
	}
	
	/**
	 * 添加图书馆缓存
	 * @param userid
	 * @param value
	 */
	public static void addLibCache(String userid, Object value) {
		CacheSupport.put(Cache_NAME, userid + LIB_SIGNAL, value);
	}
	
	/**
	 * 删除图书馆缓存
	 * @param userid
	 */
	public static void deleteLibCache(String userid) {
		CacheSupport.remove(Cache_NAME, userid + LIB_SIGNAL);
	}
	
	/**
	 * 
	 * @param userid
	 * @param c
	 * @return
	 */
	public static <T> T getLibCache(String userid, Class<T> c) {
		return CacheSupport.get(Cache_NAME, userid + LIB_SIGNAL, c);
	}
	
	/**
	 * 添加一卡通缓存
	 * @param userid
	 * @param value
	 */
	public static void addECardCache(String userid, Object value) {
		CacheSupport.put(Cache_NAME, userid + ECARD_SIGNAL, value);
	}
	
	/**
	 * 删除一卡通缓存
	 * @param userid
	 */
	public static void deleteECardCache(String userid) {
		CacheSupport.remove(Cache_NAME, userid + ECARD_SIGNAL);
		CacheSupport.remove(Cache_NAME, userid + ECARD_STATE_SINGAL);
	}
	
	/**
	 * 
	 * @param userid
	 * @param c
	 * @return
	 */
	public static <T> T getECardCache(String userid, Class<T> c) {
		return CacheSupport.get(Cache_NAME, userid + ECARD_SIGNAL, c);
	}
	
	/**
	 * 添加一卡通缓存
	 * @param userid
	 * @param value
	 */
	public static void addECRCache(String userid, Object value) {
		CacheSupport.put(Cache_NAME, userid + ECARD_REINPUT, value);
	}
	
	/**
	 * 删除一卡通缓存
	 * @param userid
	 */
	public static void deleteECRCache(String userid) {
		CacheSupport.remove(Cache_NAME, userid + ECARD_REINPUT);
	}
	
	/**
	 * 
	 * @param userid
	 * @param c
	 * @return
	 */
	public static <T> T getECRCache(String userid, Class<T> c) {
		return CacheSupport.get(Cache_NAME, userid + ECARD_REINPUT, c);
	}
	
	/**
	 * 删除一卡通视图缓存
	 * @param userid
	 */
	public static void deleteECardViewCache(String userid) {
		CacheSupport.remove(Cache_NAME, userid + ECARD_STATE_SINGAL);
	}
	
	/**
	 * 
	 * @param userid
	 * @param c
	 * @return
	 */
	public static <T> T getECardViewCache(String userid, Class<T> c) {
		return CacheSupport.get(Cache_NAME, userid + ECARD_STATE_SINGAL, c);
	}
	
	/**
	 * 添加一卡通视图缓存
	 * @param userid
	 * @param value
	 */
	public static void addECardViewCache(String userid, Object value) {
		CacheSupport.put(Cache_NAME, userid + ECARD_STATE_SINGAL, value);
	}
	
	
	
	//日历对象，记录红花数
	private static Calendar calendar = Calendar.getInstance();
	private static int flowerRestrict = Integer.valueOf(PropertySupport.getProperty("flower.restrict"));
	/**
	 * 限制每天的小红花数，返回应该向数据库中更新的数量
	 * @param userid
	 * @param count
	 * @return
	 */
	public static int flowerIncrease(String userid, int count) {
		int realCount = 0;
		
		int nowDate = calendar.get(Calendar.DATE);
		
		Integer nowCount = CacheSupport.get(Cache_NAME, userid + FLOWER_SIGNAL, Integer.class);
		int date = 0;
		int sum = 0;
		int oldSum = 0;
		if(nowCount != null) {
			date = nowCount / 100;
			sum = nowCount % 100;
			oldSum = sum;
			if(date != nowDate) {
				date = nowDate;
				sum = count > flowerRestrict ? flowerRestrict : count; //不能超过
			} else {
				int newSum = sum + count;
				sum = newSum > flowerRestrict ? flowerRestrict : newSum;
			}
		} else {
			sum = count > flowerRestrict ? flowerRestrict : count;
		}
		
		//更新flower记录
		CacheSupport.put(Cache_NAME, userid + FLOWER_SIGNAL, date * 100 + sum);
//		map.put(userid + FLOWER_SIGNAL, date * 100 + sum);
		//计算真正需要在数据库中添加的数量
		realCount =  (oldSum == 0 ? sum : (sum - oldSum));
		
		return realCount;
	}
}
